package com.hailin.pigauth.service;

import com.hailin.pigapi.common.Result;
import com.hailin.pigapi.model.auth.UserInfoDto;
import com.hailin.pigapi.request.auth.UserInfoRequest;
import com.hailin.pigapi.request.auth.UserRegistRequest;
import com.hailin.pigapi.utils.MD5Utils;
import com.hailin.pigauth.dao.PigAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "pigAuthService")
public class PigAuthService {

    @Autowired
    private PigAuthMapper pigAuthMapper;

    /**
     * 用户登录
     * @param userInfoRequest
     * @return
     */
    public Result login(UserInfoRequest userInfoRequest) {
        Result result = Result.create();
        userInfoRequest.setUserPass(MD5Utils.MD5(userInfoRequest.getUserPass()));
        int count = pigAuthMapper.login(userInfoRequest);
        if(count < 1){
            return result.fail("用户名或密码错误");
        }
        return result.success();
    }

    /**
     * 用户注册
     * @param userRegistRequest
     * @return
     */
    public Result regist(UserRegistRequest userRegistRequest) {
        Result result = Result.create();
        int count = pigAuthMapper.userIsExist(userRegistRequest);
        if(count > 0){
            return result.fail("用户名/手机号/邮箱已被注册");
        }
        userRegistRequest.setUserPass(MD5Utils.MD5(userRegistRequest.getUserPass()));
        pigAuthMapper.regist(userRegistRequest);
        return result.success();
    }

    /**
     * 根据di查询用户
     * @param userId
     * @return
     */
    public Result<UserInfoDto> getUserById(String userId) {
        Result<UserInfoDto> result = Result.create();
        UserInfoDto userInfoDto = pigAuthMapper.getUserById(userId);
        return result.success(userInfoDto);
    }
}
